<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.11: http://docutils.sourceforge.net/" />
<title>100 numpy exercises</title>
<link rel="stylesheet" href="numpy.css" type="text/css" />
</head>
<body>
<div class="document" id="numpy-exercises">
<h1 class="title">100 numpy exercises</h1>
<h2 class="subtitle" id="a-joint-effort-of-the-numpy-community">A joint effort of the numpy community</h2>

<p>The goal is both to offer a quick reference for new and old users and to
provide also a set of exercices for those who teach. If you remember having
asked or answered a (short) problem, you can send a pull request. The format
is:</p>
<pre class="literal-block">
#. Find indices of non-zero elements from [1,2,0,0,4,0]

   .. code:: python

      # Author: Somebody

      print np.nonzero([1,2,0,0,4,0])
</pre>
<p>Here is what the page looks like so far:
<a class="reference external" href="http://www.loria.fr/~rougier/teaching/numpy.100/index.html">http://www.loria.fr/~rougier/teaching/numpy.100/index.html</a></p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">The level names came from an old-game (Dungeon Master)</p>
</div>
<p>Repository is at: <a class="reference external" href="https://github.com/rougier/numpy-100">https://github.com/rougier/numpy-100</a></p>
<p>The corresponding <a class="reference external" href="https://github.com/rougier/numpy-100/blob/master/README.ipynb">IPython notebook</a> is available
from the github repo, thanks to the <a class="reference external" href="https://github.com/esc/rst2ipynb">rst2ipynb</a> conversion tool by <a class="reference external" href="http://haenel.co">Valentin Haenel</a></p>
<p>Thanks to Michiaki Ariga, there is now a
<a class="reference external" href="https://github.com/chezou/julia-100-exercises">Julia version</a>.</p>
<div class="section" id="neophyte">
<h1>Neophyte</h1>
<ol class="arabic">
<li><p class="first">Import the numpy package under the name <tt class="docutils literal">np</tt></p>
<pre class="code python literal-block">
<span class="keyword namespace">import</span> <span class="name namespace">numpy</span> <span class="keyword namespace">as</span> <span class="name namespace">np</span>
</pre>
</li>
<li><p class="first">Print the numpy version and the configuration.</p>
<pre class="code python literal-block">
<span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">__version__</span>
<span class="name">np</span><span class="operator">.</span><span class="name">__config__</span><span class="operator">.</span><span class="name">show</span><span class="punctuation">()</span>
</pre>
</li>
<li><p class="first">Create a null vector of size 10</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a null vector of size 10 but the fifth value which is 1</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="name">Z</span><span class="punctuation">[</span><span class="literal number integer">4</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a vector with values ranging from 10 to 49</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">50</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a 3x3 matrix with values ranging from 0 to 8</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">9</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Find indices of non-zero elements from [1,2,0,0,4,0]</p>
<pre class="code python literal-block">
<span class="name">nz</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">nonzero</span><span class="punctuation">([</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">])</span>
<span class="keyword">print</span> <span class="name">nz</span>
</pre>
</li>
<li><p class="first">Create a 3x3 identity matrix</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">eye</span><span class="punctuation">(</span><span class="literal number integer">3</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a 5x5 matrix with values 1,2,3,4 just below the diagonal</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">diag</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="operator">+</span><span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">4</span><span class="punctuation">),</span><span class="name">k</span><span class="operator">=-</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a 3x3x3 array with random values</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">((</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="novice">
<h1>Novice</h1>
<ol class="arabic">
<li><p class="first">Create a 8x8 matrix and fill it with a checkerboard pattern</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">((</span><span class="literal number integer">8</span><span class="punctuation">,</span><span class="literal number integer">8</span><span class="punctuation">),</span><span class="name">dtype</span><span class="operator">=</span><span class="name builtin">int</span><span class="punctuation">)</span>
<span class="name">Z</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">::</span><span class="literal number integer">2</span><span class="punctuation">,::</span><span class="literal number integer">2</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span>
<span class="name">Z</span><span class="punctuation">[::</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">::</span><span class="literal number integer">2</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a 10x10 array with random values and find the minimum and maximum values</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">((</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
<span class="name">Zmin</span><span class="punctuation">,</span> <span class="name">Zmax</span> <span class="operator">=</span> <span class="name">Z</span><span class="operator">.</span><span class="name">min</span><span class="punctuation">(),</span> <span class="name">Z</span><span class="operator">.</span><span class="name">max</span><span class="punctuation">()</span>
<span class="keyword">print</span> <span class="name">Zmin</span><span class="punctuation">,</span> <span class="name">Zmax</span>
</pre>
</li>
<li><p class="first">Create a checkerboard 8x8 matrix using the tile function</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">tile</span><span class="punctuation">(</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">([[</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">],[</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">]]),</span> <span class="punctuation">(</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Normalize a 5x5 random matrix (between 0 and 1)</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">((</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">))</span>
<span class="name">Zmax</span><span class="punctuation">,</span><span class="name">Zmin</span> <span class="operator">=</span> <span class="name">Z</span><span class="operator">.</span><span class="name">max</span><span class="punctuation">(),</span> <span class="name">Z</span><span class="operator">.</span><span class="name">min</span><span class="punctuation">()</span>
<span class="name">Z</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">Z</span> <span class="operator">-</span> <span class="name">Zmin</span><span class="punctuation">)</span><span class="operator">/</span><span class="punctuation">(</span><span class="name">Zmax</span> <span class="operator">-</span> <span class="name">Zmin</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Multiply a 5x3 matrix by a 3x2 matrix (real matrix product)</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">dot</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">)),</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">)))</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a 5x5 matrix with row values ranging from 0 to 4</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">((</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">))</span>
<span class="name">Z</span> <span class="operator">+=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">5</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a vector of size 10 with values ranging from 0 to 1, both excluded</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">12</span><span class="punctuation">,</span><span class="name">endpoint</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)[</span><span class="literal number integer">1</span><span class="punctuation">:</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">]</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a random vector of size 10 and sort it</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="name">Z</span><span class="operator">.</span><span class="name">sort</span><span class="punctuation">()</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Consider two random array A anb B, check if they are equal.</p>
<pre class="code python literal-block">
<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">)</span>
<span class="name">B</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">)</span>
<span class="name">equal</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">allclose</span><span class="punctuation">(</span><span class="name">A</span><span class="punctuation">,</span><span class="name">B</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">equal</span>
</pre>
</li>
<li><p class="first">Create a random vector of size 30 and find the mean value</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">(</span><span class="literal number integer">30</span><span class="punctuation">)</span>
<span class="name">m</span> <span class="operator">=</span> <span class="name">Z</span><span class="operator">.</span><span class="name">mean</span><span class="punctuation">()</span>
<span class="keyword">print</span> <span class="name">m</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="apprentice">
<h1>Apprentice</h1>
<ol class="arabic">
<li><p class="first">Make an array immutable (read-only)</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="name">Z</span><span class="operator">.</span><span class="name">flags</span><span class="operator">.</span><span class="name">writeable</span> <span class="operator">=</span> <span class="name builtin pseudo">False</span>
<span class="name">Z</span><span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">1</span>
</pre>
</li>
<li><p class="first">Consider a random 10x2 matrix representing cartesian coordinates, convert
them to polar coordinates</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">((</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>
<span class="name">X</span><span class="punctuation">,</span><span class="name">Y</span> <span class="operator">=</span> <span class="name">Z</span><span class="punctuation">[:,</span><span class="literal number integer">0</span><span class="punctuation">],</span> <span class="name">Z</span><span class="punctuation">[:,</span><span class="literal number integer">1</span><span class="punctuation">]</span>
<span class="name">R</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">sqrt</span><span class="punctuation">(</span><span class="name">X</span><span class="operator">**</span><span class="literal number integer">2</span><span class="operator">+</span><span class="name">Y</span><span class="operator">**</span><span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="name">T</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arctan2</span><span class="punctuation">(</span><span class="name">Y</span><span class="punctuation">,</span><span class="name">X</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">R</span>
<span class="keyword">print</span> <span class="name">T</span>
</pre>
</li>
<li><p class="first">Create random vector of size 10 and replace the maximum value by 0</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="name">Z</span><span class="punctuation">[</span><span class="name">Z</span><span class="operator">.</span><span class="name">argmax</span><span class="punctuation">()]</span> <span class="operator">=</span> <span class="literal number integer">0</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Create a structured array with <tt class="docutils literal">x</tt> and <tt class="docutils literal">y</tt> coordinates covering the
[0,1]x[0,1] area.</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">((</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">),</span> <span class="punctuation">[(</span><span class="literal string">'x'</span><span class="punctuation">,</span><span class="name builtin">float</span><span class="punctuation">),(</span><span class="literal string">'y'</span><span class="punctuation">,</span><span class="name builtin">float</span><span class="punctuation">)])</span>
<span class="name">Z</span><span class="punctuation">[</span><span class="literal string">'x'</span><span class="punctuation">],</span> <span class="name">Z</span><span class="punctuation">[</span><span class="literal string">'y'</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">meshgrid</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">),</span>
                             <span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Print the minimum and maximum representable value for each numpy scalar type</p>
<pre class="code python literal-block">
<span class="keyword">for</span> <span class="name">dtype</span> <span class="operator word">in</span> <span class="punctuation">[</span><span class="name">np</span><span class="operator">.</span><span class="name">int8</span><span class="punctuation">,</span> <span class="name">np</span><span class="operator">.</span><span class="name">int32</span><span class="punctuation">,</span> <span class="name">np</span><span class="operator">.</span><span class="name">int64</span><span class="punctuation">]:</span>
   <span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">iinfo</span><span class="punctuation">(</span><span class="name">dtype</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">min</span>
   <span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">iinfo</span><span class="punctuation">(</span><span class="name">dtype</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">max</span>
<span class="keyword">for</span> <span class="name">dtype</span> <span class="operator word">in</span> <span class="punctuation">[</span><span class="name">np</span><span class="operator">.</span><span class="name">float32</span><span class="punctuation">,</span> <span class="name">np</span><span class="operator">.</span><span class="name">float64</span><span class="punctuation">]:</span>
   <span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">finfo</span><span class="punctuation">(</span><span class="name">dtype</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">min</span>
   <span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">finfo</span><span class="punctuation">(</span><span class="name">dtype</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">max</span>
   <span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">finfo</span><span class="punctuation">(</span><span class="name">dtype</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">eps</span>
</pre>
</li>
<li><p class="first">Create a structured array representing a position (x,y) and a color (r,g,b)</p>
<pre class="code python literal-block">
 <span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">,</span> <span class="punctuation">[</span> <span class="punctuation">(</span><span class="literal string">'position'</span><span class="punctuation">,</span> <span class="punctuation">[</span> <span class="punctuation">(</span><span class="literal string">'x'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">),</span>
                                   <span class="punctuation">(</span><span class="literal string">'y'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">)]),</span>
                    <span class="punctuation">(</span><span class="literal string">'color'</span><span class="punctuation">,</span>    <span class="punctuation">[</span> <span class="punctuation">(</span><span class="literal string">'r'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">),</span>
                                   <span class="punctuation">(</span><span class="literal string">'g'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">),</span>
                                   <span class="punctuation">(</span><span class="literal string">'b'</span><span class="punctuation">,</span> <span class="name builtin">float</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">)])])</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Consider a random vector with shape (100,2) representing coordinates, find
point by point distances</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">((</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>
<span class="name">X</span><span class="punctuation">,</span><span class="name">Y</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">atleast_2d</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">[:,</span><span class="literal number integer">0</span><span class="punctuation">]),</span> <span class="name">np</span><span class="operator">.</span><span class="name">atleast_2d</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">[:,</span><span class="literal number integer">1</span><span class="punctuation">])</span>
<span class="name">D</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">sqrt</span><span class="punctuation">(</span> <span class="punctuation">(</span><span class="name">X</span><span class="operator">-</span><span class="name">X</span><span class="operator">.</span><span class="name">T</span><span class="punctuation">)</span><span class="operator">**</span><span class="literal number integer">2</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="name">Y</span><span class="operator">-</span><span class="name">Y</span><span class="operator">.</span><span class="name">T</span><span class="punctuation">)</span><span class="operator">**</span><span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">D</span>

<span class="comment"># Much faster with scipy</span>
<span class="keyword namespace">import</span> <span class="name namespace">scipy</span>
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">random</span><span class="punctuation">((</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>
<span class="name">D</span> <span class="operator">=</span> <span class="name">scipy</span><span class="operator">.</span><span class="name">spatial</span><span class="operator">.</span><span class="name">distance</span><span class="operator">.</span><span class="name">cdist</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">,</span><span class="name">Z</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">D</span>
</pre>
</li>
<li><p class="first">Generate a generic 2D Gaussian-like array</p>
<pre class="code python literal-block">
<span class="name">X</span><span class="punctuation">,</span> <span class="name">Y</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">meshgrid</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">),</span> <span class="name">np</span><span class="operator">.</span><span class="name">linspace</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
<span class="name">D</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">sqrt</span><span class="punctuation">(</span><span class="name">X</span><span class="operator">*</span><span class="name">X</span><span class="operator">+</span><span class="name">Y</span><span class="operator">*</span><span class="name">Y</span><span class="punctuation">)</span>
<span class="name">sigma</span><span class="punctuation">,</span> <span class="name">mu</span> <span class="operator">=</span> <span class="literal number float">1.0</span><span class="punctuation">,</span> <span class="literal number float">0.0</span>
<span class="name">G</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">exp</span><span class="punctuation">(</span><span class="operator">-</span><span class="punctuation">(</span> <span class="punctuation">(</span><span class="name">D</span><span class="operator">-</span><span class="name">mu</span><span class="punctuation">)</span><span class="operator">**</span><span class="literal number integer">2</span> <span class="operator">/</span> <span class="punctuation">(</span> <span class="literal number float">2.0</span> <span class="operator">*</span> <span class="name">sigma</span><span class="operator">**</span><span class="literal number integer">2</span> <span class="punctuation">)</span> <span class="punctuation">)</span> <span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">G</span>
</pre>
</li>
<li><p class="first">How to tell if a given 2D array has null columns ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Warren Weckesser</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,(</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="punctuation">(</span><span class="operator">~</span><span class="name">Z</span><span class="operator">.</span><span class="name">any</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">0</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">any</span><span class="punctuation">()</span>
</pre>
</li>
<li><p class="first">Find the nearest value from a given value in an array</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">uniform</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="name">z</span> <span class="operator">=</span> <span class="literal number float">0.5</span>
<span class="name">m</span> <span class="operator">=</span> <span class="name">Z</span><span class="operator">.</span><span class="name">flat</span><span class="punctuation">[</span><span class="name">np</span><span class="operator">.</span><span class="name">abs</span><span class="punctuation">(</span><span class="name">Z</span> <span class="operator">-</span> <span class="name">z</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">argmin</span><span class="punctuation">()]</span>
<span class="keyword">print</span> <span class="name">m</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="journeyman">
<h1>Journeyman</h1>
<ol class="arabic">
<li><p class="first">Consider the following file:</p>
<pre class="literal-block">
1,2,3,4,5
6,,,7,8
,,9,10,11
</pre>
<p>How to read it ?</p>
<pre class="code python literal-block">
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">genfromtxt</span><span class="punctuation">(</span><span class="literal string">&quot;missing.dat&quot;</span><span class="punctuation">,</span> <span class="name">delimiter</span><span class="operator">=</span><span class="literal string">&quot;,&quot;</span><span class="punctuation">)</span>
</pre>
</li>
<li><p class="first">Consider a generator function that generates 10 integers and use it to build an
array</p>
<pre class="code python literal-block">
<span class="keyword">def</span> <span class="name function">generate</span><span class="punctuation">():</span>
    <span class="keyword">for</span> <span class="name">x</span> <span class="operator word">in</span> <span class="name builtin">xrange</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">):</span>
        <span class="keyword">yield</span> <span class="name">x</span>
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">fromiter</span><span class="punctuation">(</span><span class="name">generate</span><span class="punctuation">(),</span><span class="name">dtype</span><span class="operator">=</span><span class="name builtin">float</span><span class="punctuation">,</span><span class="name">count</span><span class="operator">=-</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Consider a given vector, how to add 1 to each element indexed by a second
vector (be careful with repeated indices) ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Brett Olsen</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">)</span>
<span class="name">I</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">),</span><span class="literal number integer">20</span><span class="punctuation">)</span>
<span class="name">Z</span> <span class="operator">+=</span> <span class="name">np</span><span class="operator">.</span><span class="name">bincount</span><span class="punctuation">(</span><span class="name">I</span><span class="punctuation">,</span> <span class="name">minlength</span><span class="operator">=</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">How to accumulate elements of a vector (X) to an array (F) based on an index
list (I) ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Alan G Isaac</span>

<span class="name">X</span> <span class="operator">=</span> <span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">6</span><span class="punctuation">]</span>
<span class="name">I</span> <span class="operator">=</span> <span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">9</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">]</span>
<span class="name">F</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">bincount</span><span class="punctuation">(</span><span class="name">I</span><span class="punctuation">,</span><span class="name">X</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">F</span>
</pre>
</li>
<li><p class="first">Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique
colors</p>
<pre class="code python literal-block">
<span class="comment"># Author: Nadav Horesh</span>

<span class="name">w</span><span class="punctuation">,</span><span class="name">h</span> <span class="operator">=</span> <span class="literal number integer">16</span><span class="punctuation">,</span><span class="literal number integer">16</span>
<span class="name">I</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,(</span><span class="name">h</span><span class="punctuation">,</span><span class="name">w</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">astype</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">ubyte</span><span class="punctuation">)</span>
<span class="name">F</span> <span class="operator">=</span> <span class="name">I</span><span class="punctuation">[</span><span class="operator">...</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">]</span><span class="operator">*</span><span class="literal number integer">256</span><span class="operator">*</span><span class="literal number integer">256</span> <span class="operator">+</span> <span class="name">I</span><span class="punctuation">[</span><span class="operator">...</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">]</span><span class="operator">*</span><span class="literal number integer">256</span> <span class="operator">+</span><span class="name">I</span><span class="punctuation">[</span><span class="operator">...</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">]</span>
<span class="name">n</span> <span class="operator">=</span> <span class="name builtin">len</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">unique</span><span class="punctuation">(</span><span class="name">F</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">unique</span><span class="punctuation">(</span><span class="name">I</span><span class="punctuation">)</span>
</pre>
</li>
<li><p class="first">Considering a four dimensions array, how to get sum over the last two axis at once ?</p>
<pre class="code python literal-block">
<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">,(</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">))</span>
<span class="name builtin">sum</span> <span class="operator">=</span> <span class="name">A</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="name">A</span><span class="operator">.</span><span class="name">shape</span><span class="punctuation">[:</span><span class="operator">-</span><span class="literal number integer">2</span><span class="punctuation">]</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">,))</span><span class="operator">.</span><span class="name">sum</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=-</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="keyword">print</span>
</pre>
</li>
<li><p class="first">Considering a one-dimensional vector D, how to compute means of subsets of D
using a vector S of same size describing subset indices ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Jaime Fernández del Río</span>

<span class="name">D</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">uniform</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">100</span><span class="punctuation">)</span>
<span class="name">S</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">100</span><span class="punctuation">)</span>
<span class="name">D_sums</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">bincount</span><span class="punctuation">(</span><span class="name">S</span><span class="punctuation">,</span> <span class="name">weights</span><span class="operator">=</span><span class="name">D</span><span class="punctuation">)</span>
<span class="name">D_counts</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">bincount</span><span class="punctuation">(</span><span class="name">S</span><span class="punctuation">)</span>
<span class="name">D_means</span> <span class="operator">=</span> <span class="name">D_sums</span> <span class="operator">/</span> <span class="name">D_counts</span>
<span class="keyword">print</span> <span class="name">D_means</span>
</pre>
</li>
<li><p class="first">Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3
consecutive zeros interleaved between each value ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Warren Weckesser</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">([</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">])</span>
<span class="name">nz</span> <span class="operator">=</span> <span class="literal number integer">3</span>
<span class="name">Z0</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">)</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">)</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">)</span><span class="operator">*</span><span class="punctuation">(</span><span class="name">nz</span><span class="punctuation">))</span>
<span class="name">Z0</span><span class="punctuation">[::</span><span class="name">nz</span><span class="operator">+</span><span class="literal number integer">1</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">Z</span>
<span class="keyword">print</span> <span class="name">Z0</span>
</pre>
</li>
<li><p class="first">Consider an array of dimension (5,5,3), how to mulitply it by an array with
dimensions (5,5) ?</p>
<pre class="code python literal-block">
<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span>
<span class="name">B</span> <span class="operator">=</span> <span class="literal number integer">2</span><span class="operator">*</span><span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">A</span> <span class="operator">*</span> <span class="name">B</span><span class="punctuation">[:,:,</span><span class="name builtin pseudo">None</span><span class="punctuation">]</span>
</pre>
</li>
<li><p class="first">How to swap two rows of an array ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Eelco Hoogendoorn</span>

<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">25</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">)</span>
<span class="name">A</span><span class="punctuation">[[</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">]]</span> <span class="operator">=</span> <span class="name">A</span><span class="punctuation">[[</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">]]</span>
<span class="keyword">print</span> <span class="name">A</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="craftsman">
<h1>Craftsman</h1>
<ol class="arabic">
<li><p class="first">Consider a one-dimensional array Z, build a two-dimensional array whose
first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last
row should be (Z[-3],Z[-2],Z[-1])</p>
<pre class="code python literal-block">
<span class="comment"># Author: Joe Kington / Erik Rigtorp</span>
<span class="keyword namespace">from</span> <span class="name namespace">numpy.lib</span> <span class="keyword namespace">import</span> <span class="name">stride_tricks</span>

<span class="keyword">def</span> <span class="name function">rolling</span><span class="punctuation">(</span><span class="name">a</span><span class="punctuation">,</span> <span class="name">window</span><span class="punctuation">):</span>
    <span class="name">shape</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">a</span><span class="operator">.</span><span class="name">size</span> <span class="operator">-</span> <span class="name">window</span> <span class="operator">+</span> <span class="literal number integer">1</span><span class="punctuation">,</span> <span class="name">window</span><span class="punctuation">)</span>
    <span class="name">strides</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">a</span><span class="operator">.</span><span class="name">itemsize</span><span class="punctuation">,</span> <span class="name">a</span><span class="operator">.</span><span class="name">itemsize</span><span class="punctuation">)</span>
    <span class="keyword">return</span> <span class="name">stride_tricks</span><span class="operator">.</span><span class="name">as_strided</span><span class="punctuation">(</span><span class="name">a</span><span class="punctuation">,</span> <span class="name">shape</span><span class="operator">=</span><span class="name">shape</span><span class="punctuation">,</span> <span class="name">strides</span><span class="operator">=</span><span class="name">strides</span><span class="punctuation">)</span>
<span class="name">Z</span> <span class="operator">=</span> <span class="name">rolling</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">10</span><span class="punctuation">),</span> <span class="literal number integer">3</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">Z</span>
</pre>
</li>
<li><p class="first">Consider a set of 10 triplets describing 10 triangles (with shared
vertices), find the set of unique line segments composing all the triangles.</p>
<pre class="code python literal-block">
<span class="comment"># Author: Nicolas P. Rougier</span>

<span class="name">faces</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">100</span><span class="punctuation">,(</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span>
<span class="name">F</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">roll</span><span class="punctuation">(</span><span class="name">faces</span><span class="operator">.</span><span class="name">repeat</span><span class="punctuation">(</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">),</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="name">F</span> <span class="operator">=</span> <span class="name">F</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">F</span><span class="punctuation">)</span><span class="operator">*</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="name">F</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">sort</span><span class="punctuation">(</span><span class="name">F</span><span class="punctuation">,</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="name">G</span> <span class="operator">=</span> <span class="name">F</span><span class="operator">.</span><span class="name">view</span><span class="punctuation">(</span> <span class="name">dtype</span><span class="operator">=</span><span class="punctuation">[(</span><span class="literal string">'p0'</span><span class="punctuation">,</span><span class="name">F</span><span class="operator">.</span><span class="name">dtype</span><span class="punctuation">),(</span><span class="literal string">'p1'</span><span class="punctuation">,</span><span class="name">F</span><span class="operator">.</span><span class="name">dtype</span><span class="punctuation">)]</span> <span class="punctuation">)</span>
<span class="name">G</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">unique</span><span class="punctuation">(</span><span class="name">G</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">G</span>
</pre>
</li>
<li><p class="first">Given an array C that is a bincount, how to produce an array A such that
np.bincount(A) == C ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Jaime Fernández del Río</span>

<span class="name">C</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">bincount</span><span class="punctuation">([</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">6</span><span class="punctuation">])</span>
<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">repeat</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">C</span><span class="punctuation">)),</span> <span class="name">C</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">A</span>
</pre>
</li>
<li><p class="first">How to compute averages using a sliding window over an array ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Jaime Fernández del Río</span>

<span class="keyword">def</span> <span class="name function">moving_average</span><span class="punctuation">(</span><span class="name">a</span><span class="punctuation">,</span> <span class="name">n</span><span class="operator">=</span><span class="literal number integer">3</span><span class="punctuation">)</span> <span class="punctuation">:</span>
    <span class="name">ret</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">cumsum</span><span class="punctuation">(</span><span class="name">a</span><span class="punctuation">,</span> <span class="name">dtype</span><span class="operator">=</span><span class="name builtin">float</span><span class="punctuation">)</span>
    <span class="name">ret</span><span class="punctuation">[</span><span class="name">n</span><span class="punctuation">:]</span> <span class="operator">=</span> <span class="name">ret</span><span class="punctuation">[</span><span class="name">n</span><span class="punctuation">:]</span> <span class="operator">-</span> <span class="name">ret</span><span class="punctuation">[:</span><span class="operator">-</span><span class="name">n</span><span class="punctuation">]</span>
    <span class="keyword">return</span> <span class="name">ret</span><span class="punctuation">[</span><span class="name">n</span> <span class="operator">-</span> <span class="literal number integer">1</span><span class="punctuation">:]</span> <span class="operator">/</span> <span class="name">n</span>
<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">20</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">moving_average</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">,</span> <span class="name">n</span><span class="operator">=</span><span class="literal number integer">3</span><span class="punctuation">)</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="artisan">
<h1>Artisan</h1>
<ol class="arabic">
<li><p class="first">Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3])</p>
<pre class="code python literal-block">
<span class="comment"># Author: Robert Kern</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,(</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span>
<span class="name">E</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">logical_and</span><span class="operator">.</span><span class="name">reduce</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">[:,</span><span class="literal number integer">1</span><span class="punctuation">:]</span> <span class="operator">==</span> <span class="name">Z</span><span class="punctuation">[:,:</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">],</span> <span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span>
<span class="name">U</span> <span class="operator">=</span> <span class="name">Z</span><span class="punctuation">[</span><span class="operator">~</span><span class="name">E</span><span class="punctuation">]</span>
<span class="keyword">print</span> <span class="name">Z</span>
<span class="keyword">print</span> <span class="name">U</span>
</pre>
</li>
<li><p class="first">Convert a vector of ints into a matrix binary representation.</p>
<pre class="code python literal-block">
<span class="comment"># Author: Warren Weckesser</span>

<span class="name">I</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">([</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">,</span> <span class="literal number integer">15</span><span class="punctuation">,</span> <span class="literal number integer">16</span><span class="punctuation">,</span> <span class="literal number integer">32</span><span class="punctuation">,</span> <span class="literal number integer">64</span><span class="punctuation">,</span> <span class="literal number integer">128</span><span class="punctuation">])</span>
<span class="name">B</span> <span class="operator">=</span> <span class="punctuation">((</span><span class="name">I</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">)</span> <span class="operator">&amp;</span> <span class="punctuation">(</span><span class="literal number integer">2</span><span class="operator">**</span><span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">8</span><span class="punctuation">)))</span> <span class="operator">!=</span> <span class="literal number integer">0</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">astype</span><span class="punctuation">(</span><span class="name builtin">int</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">B</span><span class="punctuation">[:,::</span><span class="operator">-</span><span class="literal number integer">1</span><span class="punctuation">]</span>

<span class="comment"># Author: Daniel T. McDonald</span>

<span class="name">I</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">([</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">,</span> <span class="literal number integer">3</span><span class="punctuation">,</span> <span class="literal number integer">15</span><span class="punctuation">,</span> <span class="literal number integer">16</span><span class="punctuation">,</span> <span class="literal number integer">32</span><span class="punctuation">,</span> <span class="literal number integer">64</span><span class="punctuation">,</span> <span class="literal number integer">128</span><span class="punctuation">],</span> <span class="name">dtype</span><span class="operator">=</span><span class="name">np</span><span class="operator">.</span><span class="name">uint8</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">np</span><span class="operator">.</span><span class="name">unpackbits</span><span class="punctuation">(</span><span class="name">I</span><span class="punctuation">[:,</span> <span class="name">np</span><span class="operator">.</span><span class="name">newaxis</span><span class="punctuation">],</span> <span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span>
</pre>
</li>
<li><p class="first">Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to
compute distance from p to each line i (P0[i],P1[i]) ?</p>
<pre class="code python literal-block">
<span class="keyword">def</span> <span class="name function">distance</span><span class="punctuation">(</span><span class="name">P0</span><span class="punctuation">,</span> <span class="name">P1</span><span class="punctuation">,</span> <span class="name">p</span><span class="punctuation">):</span>
  <span class="name">T</span> <span class="operator">=</span> <span class="name">P1</span> <span class="operator">-</span> <span class="name">P0</span>
  <span class="name">L</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">T</span><span class="operator">**</span><span class="literal number integer">2</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">sum</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">)</span>
  <span class="name">U</span> <span class="operator">=</span> <span class="operator">-</span><span class="punctuation">((</span><span class="name">P0</span><span class="punctuation">[:,</span><span class="literal number integer">0</span><span class="punctuation">]</span><span class="operator">-</span><span class="name">p</span><span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">])</span><span class="operator">*</span><span class="name">T</span><span class="punctuation">[:,</span><span class="literal number integer">0</span><span class="punctuation">]</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="name">P0</span><span class="punctuation">[:,</span><span class="literal number integer">1</span><span class="punctuation">]</span><span class="operator">-</span><span class="name">p</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">])</span><span class="operator">*</span><span class="name">T</span><span class="punctuation">[:,</span><span class="literal number integer">1</span><span class="punctuation">])</span> <span class="operator">/</span> <span class="name">L</span>
  <span class="name">U</span> <span class="operator">=</span> <span class="name">U</span><span class="operator">.</span><span class="name">reshape</span><span class="punctuation">(</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">U</span><span class="punctuation">),</span><span class="literal number integer">1</span><span class="punctuation">)</span>
  <span class="name">D</span> <span class="operator">=</span> <span class="name">P0</span> <span class="operator">+</span> <span class="name">U</span><span class="operator">*</span><span class="name">T</span> <span class="operator">-</span> <span class="name">p</span>
  <span class="keyword">return</span> <span class="name">np</span><span class="operator">.</span><span class="name">sqrt</span><span class="punctuation">((</span><span class="name">D</span><span class="operator">**</span><span class="literal number integer">2</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">sum</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="literal number integer">1</span><span class="punctuation">))</span>

<span class="name">P0</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">uniform</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">,(</span><span class="literal number integer">100</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>
<span class="name">P1</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">uniform</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">,(</span><span class="literal number integer">100</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>
<span class="name">p</span>  <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">uniform</span><span class="punctuation">(</span><span class="operator">-</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">,(</span>  <span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">distance</span><span class="punctuation">(</span><span class="name">P0</span><span class="punctuation">,</span> <span class="name">P1</span><span class="punctuation">,</span> <span class="name">p</span><span class="punctuation">)</span>
</pre>
</li>
<li><p class="first">Consider 2 sets of points P0,P1 describing lines (2d) and a set of point P,
how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i]) ?</p>
<pre class="code python literal-block">
<span class="name">Answer</span> <span class="name">needed</span> <span class="name">actually</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="adept">
<h1>Adept</h1>
<ol class="arabic">
<li><p class="first">Consider an arbitrary array, write a function that extract a subpart with a
fixed shape and centered on a given element (pad with a <tt class="docutils literal">fill</tt> value when
necessary)</p>
<pre class="code python literal-block">
<span class="comment"># Author: Nicolas Rougier</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">,(</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
<span class="name">shape</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">)</span>
<span class="name">fill</span>  <span class="operator">=</span> <span class="literal number integer">0</span>
<span class="name">position</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">)</span>

<span class="name">R</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">(</span><span class="name">shape</span><span class="punctuation">,</span> <span class="name">dtype</span><span class="operator">=</span><span class="name">Z</span><span class="operator">.</span><span class="name">dtype</span><span class="punctuation">)</span><span class="operator">*</span><span class="name">fill</span>
<span class="name">P</span>  <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">(</span><span class="name builtin">list</span><span class="punctuation">(</span><span class="name">position</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">astype</span><span class="punctuation">(</span><span class="name builtin">int</span><span class="punctuation">)</span>
<span class="name">Rs</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">(</span><span class="name builtin">list</span><span class="punctuation">(</span><span class="name">R</span><span class="operator">.</span><span class="name">shape</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">astype</span><span class="punctuation">(</span><span class="name builtin">int</span><span class="punctuation">)</span>
<span class="name">Zs</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">(</span><span class="name builtin">list</span><span class="punctuation">(</span><span class="name">Z</span><span class="operator">.</span><span class="name">shape</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">astype</span><span class="punctuation">(</span><span class="name builtin">int</span><span class="punctuation">)</span>

<span class="name">R_start</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">zeros</span><span class="punctuation">((</span><span class="name builtin">len</span><span class="punctuation">(</span><span class="name">shape</span><span class="punctuation">),))</span><span class="operator">.</span><span class="name">astype</span><span class="punctuation">(</span><span class="name builtin">int</span><span class="punctuation">)</span>
<span class="name">R_stop</span>  <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">array</span><span class="punctuation">(</span><span class="name builtin">list</span><span class="punctuation">(</span><span class="name">shape</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">astype</span><span class="punctuation">(</span><span class="name builtin">int</span><span class="punctuation">)</span>
<span class="name">Z_start</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">P</span><span class="operator">-</span><span class="name">Rs</span><span class="operator">//</span><span class="literal number integer">2</span><span class="punctuation">)</span>
<span class="name">Z_stop</span>  <span class="operator">=</span> <span class="punctuation">(</span><span class="name">P</span><span class="operator">+</span><span class="name">Rs</span><span class="operator">//</span><span class="literal number integer">2</span><span class="punctuation">)</span><span class="operator">+</span><span class="name">Rs</span><span class="operator">%</span><span class="literal number integer">2</span>

<span class="name">R_start</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">R_start</span> <span class="operator">-</span> <span class="name">np</span><span class="operator">.</span><span class="name">minimum</span><span class="punctuation">(</span><span class="name">Z_start</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">tolist</span><span class="punctuation">()</span>
<span class="name">Z_start</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">maximum</span><span class="punctuation">(</span><span class="name">Z_start</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">tolist</span><span class="punctuation">()</span>
<span class="name">R_stop</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">maximum</span><span class="punctuation">(</span><span class="name">R_start</span><span class="punctuation">,</span> <span class="punctuation">(</span><span class="name">R_stop</span> <span class="operator">-</span> <span class="name">np</span><span class="operator">.</span><span class="name">maximum</span><span class="punctuation">(</span><span class="name">Z_stop</span><span class="operator">-</span><span class="name">Zs</span><span class="punctuation">,</span><span class="literal number integer">0</span><span class="punctuation">)))</span><span class="operator">.</span><span class="name">tolist</span><span class="punctuation">()</span>
<span class="name">Z_stop</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">minimum</span><span class="punctuation">(</span><span class="name">Z_stop</span><span class="punctuation">,</span><span class="name">Zs</span><span class="punctuation">))</span><span class="operator">.</span><span class="name">tolist</span><span class="punctuation">()</span>

<span class="name">r</span> <span class="operator">=</span> <span class="punctuation">[</span><span class="name builtin">slice</span><span class="punctuation">(</span><span class="name">start</span><span class="punctuation">,</span><span class="name">stop</span><span class="punctuation">)</span> <span class="keyword">for</span> <span class="name">start</span><span class="punctuation">,</span><span class="name">stop</span> <span class="operator word">in</span> <span class="name builtin">zip</span><span class="punctuation">(</span><span class="name">R_start</span><span class="punctuation">,</span><span class="name">R_stop</span><span class="punctuation">)]</span>
<span class="name">z</span> <span class="operator">=</span> <span class="punctuation">[</span><span class="name builtin">slice</span><span class="punctuation">(</span><span class="name">start</span><span class="punctuation">,</span><span class="name">stop</span><span class="punctuation">)</span> <span class="keyword">for</span> <span class="name">start</span><span class="punctuation">,</span><span class="name">stop</span> <span class="operator word">in</span> <span class="name builtin">zip</span><span class="punctuation">(</span><span class="name">Z_start</span><span class="punctuation">,</span><span class="name">Z_stop</span><span class="punctuation">)]</span>
<span class="name">R</span><span class="punctuation">[</span><span class="name">r</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="name">Z</span><span class="punctuation">[</span><span class="name">z</span><span class="punctuation">]</span>
<span class="keyword">print</span> <span class="name">Z</span>
<span class="keyword">print</span> <span class="name">R</span>
</pre>
</li>
<li><p class="first">Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an
array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Stéfan van der Walt</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">arange</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">15</span><span class="punctuation">,</span><span class="name">dtype</span><span class="operator">=</span><span class="name">uint32</span><span class="punctuation">)</span>
<span class="name">R</span> <span class="operator">=</span> <span class="name">stride_tricks</span><span class="operator">.</span><span class="name">as_strided</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">,(</span><span class="literal number integer">11</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">),(</span><span class="literal number integer">4</span><span class="punctuation">,</span><span class="literal number integer">4</span><span class="punctuation">))</span>
<span class="keyword">print</span> <span class="name">R</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="expert">
<h1>Expert</h1>
<ol class="arabic">
<li><p class="first">Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A
that contain elements of each row of B regardless of the order of the elements
in B ?</p>
<pre class="code python literal-block">
<span class="comment"># Author: Gabe Schwartz</span>

<span class="name">A</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,(</span><span class="literal number integer">8</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span>
<span class="name">B</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,(</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">))</span>

<span class="name">C</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">A</span><span class="punctuation">[</span><span class="operator">...</span><span class="punctuation">,</span> <span class="name">np</span><span class="operator">.</span><span class="name">newaxis</span><span class="punctuation">,</span> <span class="name">np</span><span class="operator">.</span><span class="name">newaxis</span><span class="punctuation">]</span> <span class="operator">==</span> <span class="name">B</span><span class="punctuation">)</span>
<span class="name">rows</span> <span class="operator">=</span> <span class="punctuation">(</span><span class="name">C</span><span class="operator">.</span><span class="name">sum</span><span class="punctuation">(</span><span class="name">axis</span><span class="operator">=</span><span class="punctuation">(</span><span class="literal number integer">1</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span> <span class="operator">&gt;=</span> <span class="name">B</span><span class="operator">.</span><span class="name">shape</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">])</span><span class="operator">.</span><span class="name">nonzero</span><span class="punctuation">()[</span><span class="literal number integer">0</span><span class="punctuation">]</span>
<span class="keyword">print</span> <span class="name">rows</span>
</pre>
</li>
<li><p class="first">Extract all the contiguous 3x3 blocks from a random 10x10 matrix.</p>
<pre class="code python literal-block">
<span class="comment"># Author: Chris Barker</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">,(</span><span class="literal number integer">10</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">))</span>
<span class="name">n</span> <span class="operator">=</span> <span class="literal number integer">3</span>
<span class="name">i</span> <span class="operator">=</span> <span class="literal number integer">1</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="name">Z</span><span class="operator">.</span><span class="name">shape</span><span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">]</span><span class="operator">-</span><span class="literal number integer">3</span><span class="punctuation">)</span>
<span class="name">j</span> <span class="operator">=</span> <span class="literal number integer">1</span> <span class="operator">+</span> <span class="punctuation">(</span><span class="name">Z</span><span class="operator">.</span><span class="name">shape</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">]</span><span class="operator">-</span><span class="literal number integer">3</span><span class="punctuation">)</span>
<span class="name">C</span> <span class="operator">=</span> <span class="name">stride_tricks</span><span class="operator">.</span><span class="name">as_strided</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">,</span> <span class="name">shape</span><span class="operator">=</span><span class="punctuation">(</span><span class="name">i</span><span class="punctuation">,</span> <span class="name">j</span><span class="punctuation">,</span> <span class="name">n</span><span class="punctuation">,</span> <span class="name">n</span><span class="punctuation">),</span> <span class="name">strides</span><span class="operator">=</span><span class="name">Z</span><span class="operator">.</span><span class="name">strides</span> <span class="operator">+</span> <span class="name">Z</span><span class="operator">.</span><span class="name">strides</span><span class="punctuation">)</span>
<span class="keyword">print</span> <span class="name">C</span>
</pre>
</li>
<li><p class="first">Create a 2D array subclass such that Z[i,j] == Z[j,i]</p>
<pre class="code python literal-block">
<span class="comment"># Author: Eric O. Lebigot</span>
<span class="comment"># Note: only works for 2d array and value setting using indices</span>

<span class="keyword">class</span> <span class="name class">Symetric</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">ndarray</span><span class="punctuation">):</span>
    <span class="keyword">def</span> <span class="name function">__setitem__</span><span class="punctuation">(</span><span class="name builtin pseudo">self</span><span class="punctuation">,</span> <span class="punctuation">(</span><span class="name">i</span><span class="punctuation">,</span><span class="name">j</span><span class="punctuation">),</span> <span class="name">value</span><span class="punctuation">):</span>
        <span class="name builtin">super</span><span class="punctuation">(</span><span class="name">Symetric</span><span class="punctuation">,</span> <span class="name builtin pseudo">self</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">__setitem__</span><span class="punctuation">((</span><span class="name">i</span><span class="punctuation">,</span><span class="name">j</span><span class="punctuation">),</span> <span class="name">value</span><span class="punctuation">)</span>
        <span class="name builtin">super</span><span class="punctuation">(</span><span class="name">Symetric</span><span class="punctuation">,</span> <span class="name builtin pseudo">self</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">__setitem__</span><span class="punctuation">((</span><span class="name">j</span><span class="punctuation">,</span><span class="name">i</span><span class="punctuation">),</span> <span class="name">value</span><span class="punctuation">)</span>

<span class="keyword">def</span> <span class="name function">symetric</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">):</span>
    <span class="keyword">return</span> <span class="name">np</span><span class="operator">.</span><span class="name">asarray</span><span class="punctuation">(</span><span class="name">Z</span> <span class="operator">+</span> <span class="name">Z</span><span class="operator">.</span><span class="name">T</span> <span class="operator">-</span> <span class="name">np</span><span class="operator">.</span><span class="name">diag</span><span class="punctuation">(</span><span class="name">Z</span><span class="operator">.</span><span class="name">diagonal</span><span class="punctuation">()))</span><span class="operator">.</span><span class="name">view</span><span class="punctuation">(</span><span class="name">Symetric</span><span class="punctuation">)</span>

<span class="name">S</span> <span class="operator">=</span> <span class="name">symetric</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">10</span><span class="punctuation">,(</span><span class="literal number integer">5</span><span class="punctuation">,</span><span class="literal number integer">5</span><span class="punctuation">)))</span>
<span class="name">S</span><span class="punctuation">[</span><span class="literal number integer">2</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">]</span> <span class="operator">=</span> <span class="literal number integer">42</span>
<span class="keyword">print</span> <span class="name">S</span>
</pre>
</li>
<li><p class="first">Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1).
How to compute the sum of of the p matrix products at once ? (result has shape (n,1))</p>
<pre class="code python literal-block">
<span class="comment"># Author: Stéfan van der Walt</span>

<span class="name">p</span><span class="punctuation">,</span> <span class="name">n</span> <span class="operator">=</span> <span class="literal number integer">10</span><span class="punctuation">,</span> <span class="literal number integer">20</span>
<span class="name">M</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="name">p</span><span class="punctuation">,</span><span class="name">n</span><span class="punctuation">,</span><span class="name">n</span><span class="punctuation">))</span>
<span class="name">V</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ones</span><span class="punctuation">((</span><span class="name">p</span><span class="punctuation">,</span><span class="name">n</span><span class="punctuation">,</span><span class="literal number integer">1</span><span class="punctuation">))</span>
<span class="name">S</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">tensordot</span><span class="punctuation">(</span><span class="name">M</span><span class="punctuation">,</span> <span class="name">V</span><span class="punctuation">,</span> <span class="name">axes</span><span class="operator">=</span><span class="punctuation">[[</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">2</span><span class="punctuation">],</span> <span class="punctuation">[</span><span class="literal number integer">0</span><span class="punctuation">,</span> <span class="literal number integer">1</span><span class="punctuation">]])</span>
<span class="keyword">print</span> <span class="name">S</span>

<span class="comment"># It works, because:</span>
<span class="comment"># M is (p,n,n)</span>
<span class="comment"># V is (p,n,1)</span>
<span class="comment"># Thus, summing over the paired axes 0 and 0 (of M and V independently),</span>
<span class="comment"># and 2 and 1, to remain with a (n,1) vector.</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="master">
<h1>Master</h1>
<ol class="arabic">
<li><p class="first">Given a two dimensional array, how to extract unique rows ?</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">See <a class="reference external" href="http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array/">stackoverflow</a> for explanations.</p>
</div>
<pre class="code python literal-block">
<span class="comment"># Author: Jaime Fernández del Río</span>

<span class="name">Z</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">random</span><span class="operator">.</span><span class="name">randint</span><span class="punctuation">(</span><span class="literal number integer">0</span><span class="punctuation">,</span><span class="literal number integer">2</span><span class="punctuation">,(</span><span class="literal number integer">6</span><span class="punctuation">,</span><span class="literal number integer">3</span><span class="punctuation">))</span>
<span class="name">T</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">ascontiguousarray</span><span class="punctuation">(</span><span class="name">Z</span><span class="punctuation">)</span><span class="operator">.</span><span class="name">view</span><span class="punctuation">(</span><span class="name">np</span><span class="operator">.</span><span class="name">dtype</span><span class="punctuation">((</span><span class="name">np</span><span class="operator">.</span><span class="name">void</span><span class="punctuation">,</span> <span class="name">Z</span><span class="operator">.</span><span class="name">dtype</span><span class="operator">.</span><span class="name">itemsize</span> <span class="operator">*</span> <span class="name">Z</span><span class="operator">.</span><span class="name">shape</span><span class="punctuation">[</span><span class="literal number integer">1</span><span class="punctuation">])))</span>
<span class="name">_</span><span class="punctuation">,</span> <span class="name">idx</span> <span class="operator">=</span> <span class="name">np</span><span class="operator">.</span><span class="name">unique</span><span class="punctuation">(</span><span class="name">T</span><span class="punctuation">,</span> <span class="name">return_index</span><span class="operator">=</span><span class="name builtin pseudo">True</span><span class="punctuation">)</span>
<span class="name">uZ</span> <span class="operator">=</span> <span class="name">Z</span><span class="punctuation">[</span><span class="name">idx</span><span class="punctuation">]</span>
<span class="keyword">print</span> <span class="name">uZ</span>
</pre>
</li>
</ol>
</div>
<div class="section" id="archmaster">
<h1>Archmaster</h1>
</div>
</div>
</body>
</html>
